package org.awenlice.playerdailyshop.data;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.saveddata.SavedData;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class PlayerShopData extends SavedData {
    private List<ItemStack> shopItems = new ArrayList<>();
    private static final Logger LOGGER = LogManager.getLogger();
    public List<ItemStack> getShopItems() {
        return shopItems;
    }

    public void setShopItems(List<ItemStack> shopItems) {
        this.shopItems = shopItems;
        setDirty(); // 标记数据为脏，需要保存
    }

    @Override
    public CompoundTag save(CompoundTag tag) {
        // 将商店数据保存到 NBT
        CompoundTag itemsTag = new CompoundTag();
        for (int i = 0; i < shopItems.size(); i++) {
            itemsTag.put("item" + i, shopItems.get(i).save(new CompoundTag()));
        }
        tag.put("shopItems", itemsTag);
        return tag;
    }

    public static PlayerShopData load(CompoundTag tag) {
        PlayerShopData data = new PlayerShopData();
        CompoundTag itemsTag = tag.getCompound("shopItems");
        for (int i = 0; i < 27; i++) { // 假设商店有 27 个槽位
            if (itemsTag.contains("item" + i)) {
                data.shopItems.add(ItemStack.of(itemsTag.getCompound("item" + i)));
            }
        }
        return data;
    }
}